package com.eschool.controller;

import com.eschool.pojo.ServerResult;
import com.eschool.pojo.UserChangeDTO;
import com.eschool.pojo.UserDAO;
import com.eschool.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Configuration
public class UserController {
    @Autowired
    UserService userService;

    @RequestMapping("/user/findAllUser")
    public ServerResult findAllUser(){
        List<UserDAO> UserList = userService.findAllUser();
        return new ServerResult(0,"success",UserList);
    }

    @RequestMapping("/user/Login")
    public ServerResult Login(Integer stuid,String password){
        UserDAO userDAO = userService.Login(password,stuid);
        if(userDAO!=null){
            return new ServerResult(0,"success",userDAO);
        }
        else{
            return new ServerResult(500,"fail",null);
        }
    }

    @RequestMapping("/user/findByID")
    public ServerResult findByID(Integer stuid){
        UserDAO userDAO = userService.findById(stuid);
        if(userDAO!=null){
            return new ServerResult(0,"success",userDAO);
        }
        else {
            return new ServerResult(200,"fail",null);
        }
    }

    @RequestMapping("/user/changeInfo")
    public ServerResult changeInfo(UserChangeDTO userChangeDTO){
        int updateRow = userService.UserChangeInfo(userChangeDTO);
        if(updateRow>0){
            return new ServerResult(0,"success",null);
        }
        else {
            return new ServerResult(200,"fail",null);
        }
    }

    @RequestMapping("/user/changePassword")
    public ServerResult changePassword(Integer stuid,String old_password,String new_password){
        int updateRow = userService.UserChangePassword(stuid,old_password,new_password);
        if(updateRow>0){
            return new ServerResult(0,"success",null);
        }
        else if(updateRow==-1){
            return new ServerResult(800,"old password is wrong",null);
        }
        else{
            return new ServerResult(200,"fail",null);
        }
    }
}
